package grape.workflow.rest.flowable.ext;

import grape.base.rest.client.role.RoleClient;
import grape.base.rest.client.user.UserClient;
import grape.base.service.role.po.Role;
import grape.common.SpringContextHolder;
import grape.common.exception.runtime.RBaseException;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.query.QueryProperty;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.GroupQuery;
import org.flowable.idm.engine.impl.GroupQueryImpl;
import org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yangwei
 * Created at 2020/6/8 20:40
 */
public class CustomGroupQueryImpl extends GroupQueryImpl {

    /**
     * 转接的组类型
     */
    public enum GroupType{
        role, // 角色
        comp, // 公司
        dept, // 部门
        post // 岗位
    }
    @Override
    public GroupQuery orderBy(QueryProperty property) {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public GroupQuery orderBy(QueryProperty property, NullHandlingOnOrder nullHandlingOnOrder) {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public GroupQuery asc() {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public GroupQuery desc() {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public Group singleResult() {
        RoleClient roleClient = SpringContextHolder.getBean(RoleClient.class);
        if (this.getId() != null) {
            Role role = roleClient.getByRoleId(this.getId());
            return convertRole(role);
        }

        return null;
    }

    @Override
    public List<Group> list() {
        RoleClient roleClient = SpringContextHolder.getBean(RoleClient.class);
        List<Role> result = new ArrayList<>();
        if (StringUtils.isNotEmpty(this.getUserId())) {
            List<Role> roleByuserId = roleClient.getByUserId(this.getUserId());
            if (roleByuserId != null) {
                result.addAll(roleByuserId);
            }
        }
        if (this.getIds() != null) {
            List<Role> byRoleIds = roleClient.getByRoleIds(this.getIds());

            if (byRoleIds != null) {
                result.addAll(byRoleIds);
            }
        }

        // 这里只考虑了角色，没有考虑其它类型
        return result.stream().map(this::convertRole).collect(Collectors.toList());
    }

    @Override
    public List<Group> listPage(int firstResult, int maxResults) {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    @Override
    public long count() {
        throw new RBaseException("not implement method.you can extends this class and then inject into spring");
    }

    private Group convertRole(Role role){
        GroupEntityImpl groupEntity = new GroupEntityImpl();
        groupEntity.setId(role.getId());
        groupEntity.setName(role.getName());
        groupEntity.setType(GroupType.role.name());
        return groupEntity;
    }
}
